package com.cly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cly.dao.ProdEsDao;
import com.cly.domain.Prod;
import com.cly.domain.ProdComm;
import com.cly.domain.User;
import com.cly.es.ProdEs;
import com.cly.feign.ProdCommUserFeign;
import com.cly.mapper.ProdCommMapper;
import com.cly.mapper.ProdMapper;
import com.cly.service.ProdCommService;
import com.cly.vo.ProdCommOverview;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Collections;

@Service
@Slf4j
public class ProdCommServiceImpl extends ServiceImpl<ProdCommMapper, ProdComm> implements ProdCommService{

    @Autowired
    private ProdCommMapper prodCommMapper;

    @Autowired
    private ProdMapper prodMapper;

    @Autowired
    private ProdEsDao prodEsDao;

    @Autowired
    private ProdCommUserFeign prodCommUserFeign;

    /**
     * 分页查询商品评论
     *
     * @param page
     * @param prodComm
     * @return
     */
    @Override
    public Page<ProdComm> findProdCommPage(Page<ProdComm> page, ProdComm prodComm) {
        Page<ProdComm> prodCommPage = null;
        //先判断用户是否输入商品名称
        String prodName = prodComm.getProdName();
        List<Long> prodIds = null;
        if (StringUtils.hasText(prodName)){
            //先查商品表 得到商品的ids select id
            List<Object> prodIdsObj = prodMapper.selectObjs(new LambdaQueryWrapper<Prod>()
                    .select(Prod::getProdId)
                    .like(Prod::getProdName,prodName)
            );
            if (!CollectionUtils.isEmpty(prodIdsObj)){
                //转换Long类型
                prodIds = prodIdsObj.stream()
                        .map(o -> Long.valueOf(o.toString()))
                        .collect(Collectors.toList());
            } else {
                //返回
                prodCommPage = new Page<>(page.getCurrent(),page.getSize());
                prodCommPage.setRecords(Collections.emptyList());
                prodCommPage.setTotal(0L);
                return prodCommPage;
            }
        }
        //不管有没有名字 都要分页查询评论
        prodCommPage = prodCommMapper.selectPage(page, new LambdaQueryWrapper<ProdComm>()
                .eq(!ObjectUtils.isEmpty(prodComm.getStatus()), ProdComm::getStatus, prodComm.getStatus())
                .in(!CollectionUtils.isEmpty(prodIds), ProdComm::getProdId, prodIds)
                .orderByDesc(ProdComm::getRecTime)
        );
        //组装商品名称
        List<ProdComm> prodCommList = prodCommPage.getRecords();
        //再拿一个  prodIds
        List<Long> finalProdIds = prodCommList.stream()
                .map(ProdComm::getProdId)
                .collect(Collectors.toList());
        //查询商品表
        List<Prod> prodList = prodMapper.selectBatchIds(finalProdIds);
        //组装
        //循环评论的集合
        prodCommList.forEach(prodComm1 -> {
            //精确匹配一个商品
            Prod prod1 = prodList.stream()
                    .filter(prod -> prod.getProdId().equals(prodComm1.getProdId()))
                    .collect(Collectors.toList())
                    .get(0);
            //设置名字
            prodComm1.setProdName(prod1.getProdName());
        });
        return prodCommPage;
    }

    /**
     * 根据商品id查询商品评论总览
     *
     * @param prodId
     * @return
     */
    @Override
    public ProdCommOverview getProdCommOverview(Long prodId) {
        //好评率
        ProdEs prodEs = prodEsDao.findById(prodId).get();
        BigDecimal positiveRating = prodEs.getPositiveRating();
        //好评数
        Long praiseNumber = prodEs.getPraiseNumber();
        // 总评 中评 差评 带图 查询数据库
        Integer allNum = prodCommMapper.selectCount(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
        );

        Integer secondNum = prodCommMapper.selectCount(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getEvaluate, 1)
        );

        Integer badNum = prodCommMapper.selectCount(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getEvaluate, 2)
        );

        Integer picNum = prodCommMapper.selectCount(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getEvaluate, 3)
        );

        ProdCommOverview prodCommOverview = ProdCommOverview.builder()
                .number(allNum)
                .praiseNumber(Integer.parseInt(praiseNumber.toString()))
                .picNumber(picNum)
                .negativeNumber(badNum)
                .secondaryNumber(secondNum)
                .positiveRating(positiveRating)
                .build();
        return prodCommOverview;

    }

    /**
     * 根据商品id分页查询商品评论
     * 1.分页查询评论
     * 2.远程调用memder 获得用户名和头像
     * 3.组装返回
     *
     * @param prodId
     * @param page
     * @param evaluate
     * @return
     */
    @Override
    public Page<ProdComm> getProdCommPage(Long prodId, Page<ProdComm> page, Integer evaluate) {

        Page<ProdComm> prodCommPage = prodCommMapper.selectPage(page, new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getStatus,1)
                .eq(evaluate != -1,ProdComm::getEvaluate, evaluate)
        );
        List<ProdComm> prodCommList = prodCommPage.getRecords();
        if (CollectionUtils.isEmpty(prodCommList)){
            return prodCommPage;
        }
        //需远程调用拿到用户消息，要先拿到用户的id集合
        List<String> userIds = prodCommList.stream()
                .map(ProdComm::getUserId)
                .collect(Collectors.toList());
        List<User> userList = prodCommUserFeign.getUserList(userIds);
        if (!CollectionUtils.isEmpty(userList)){
            //循环评论 组装数据
            prodCommList.forEach(prodComm -> {
                User user1 = userList.stream()
                        .filter(user -> user.getUserId().equals(prodComm.getUserId()))
                        .collect(Collectors.toList())
                        .get(0);
                //设置用户的头像和昵称
                prodComm.setNickName(user1.getNickName());
                prodComm.setPic(user1.getPic());
            });
        }
        return prodCommPage;

    }
}
